package com.yummy.convention.http;

import com.alibaba.fastjson.JSON;

public class Response<T> {

	/**
	 * 成功还是失败
	 */
	private Boolean success;
	/**
	 * 错误码
	 */
	private String code;
	/**
	 * 错误信息
	 */
	private String msg;
	/**
	 * 数据
	 */
	private T data;

	public Response() {
		
	}
	
	private Response(Boolean success, String code, String msg, T data) {
		super();
		this.success = success;
		this.code = code;
		this.msg = msg;
		this.data = data;
	}
	
	public static<T> Response<T> newResponse(Boolean success,ResponseCode rc,T data) {
		return new Response<T>(success,rc.getCode(),rc.getMsg(),data);
	}
	
	public static<T> Response<T> success(T data) {
		return newResponse(true,DefaultResponseCode.SUCCESS,data);
	}

	public static<T> Response<T> success(String msg,T data) {
		return new Response<T>(true,DefaultResponseCode.SUCCESS.getCode(),msg,data);
	}

	public static<T> Response<T> success() {
		return success(null);
	}
	
	public static<T> Response<T> failure(ResponseCode rc,T data) {
		return newResponse(false,rc,data);
	}
	
	public static<T> Response<T> failure(ResponseCode rc) {
		return newResponse(false,rc,null);
	}
	
	public static<T> Response<T> failure(String code,String msg,T data) {
		return new Response<T>(false,code,msg,data);
	}
	
	public static<T> Response<T> failure(String code,String msg) {
		return failure(code,msg,null);
	}
	
	public static<T> Response<T> failure(String msg) {
		return failure(DefaultResponseCode.FAILURE.getCode(),msg);
	}
	
	public static<T> Response<T> failure(String msg,T data) {
		return failure(DefaultResponseCode.FAILURE.getCode(),msg,data);
	}
	
	public static<T> Response<T> failure(T data) {
		return newResponse(false,DefaultResponseCode.FAILURE,data);
	}
	
	public static<T> Response<T> failure() {
		return failure(DefaultResponseCode.FAILURE.getMsg());
	}
	
	public Boolean getSuccess() {
		return success;
	}

	public void setSuccess(Boolean success) {
		this.success = success;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}
	
	public String toString() {
		return JSON.toJSONString(this);
	}
	
}
